package LeetCode;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Twelve
 */
public class findOddNum {
    public static int eor = 0;
    public static int onlyOne = 0;
    public static int onlyTwo = 0;

    public static void findOddNum1(int[] arr) {
        eor = 0;
        for (int j : arr) {
            eor ^= j;
        }
    }

    public static void findOddNum2(int[] arr) {
        onlyOne = 0;
        findOddNum1(arr);
        // err1 = a^b != 0
        int rightOne = eor & (~eor + 1);
        // 找到最右边的1

        for (int j : arr) {
            if ((rightOne & j) == 0) {
                onlyOne ^= j;
            }
        }
        onlyTwo = eor ^onlyOne ;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try (scanner) {
            System.out.println("请输入整数数组（用空格分隔）：");
            String input = scanner.nextLine();
            int[] arr = Arrays.stream(input
                    //.trim()
                    //.split("\\s+"))
                    .split(" "))
                    .filter(s -> !s.isEmpty())
                    .mapToInt(Integer::parseInt)
                    .toArray();

            if (arr.length == 0) {
                System.out.println("请输入有效的数字！");
                return;
            }

            findOddNum2(arr);
            System.out.println("两个出现奇数次的数是：" + onlyOne + " 和 " + onlyTwo);

        } catch (NumberFormatException e) {
            System.out.println("输入包含非数字字符！");
        }
    }
}